package com.leyou.auth.web;

import com.leyou.auth.service.UserAuthService;
import com.netflix.client.IResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class UserAuthController {

    @Autowired
    private UserAuthService userAuthService;

    /**
     * 用户登录
     * @param username 用户名
     * @param password 密码
     * @param response 返回客户端数据
     * @return  void
     */
    @PostMapping("/login")
    public ResponseEntity<Void> login(@RequestParam("username") String username,@RequestParam("password") String password,
                                        HttpServletResponse response){

        //调用方法 验证码登录
        userAuthService.login(username,password,response);
        //返回结果
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /**
     * 退出登录
     * @param request 请求参数
     * @param response 响应数据，写cookie用
     * @return 无
     */
    @PostMapping("/logout")
    public ResponseEntity<Void> logout(HttpServletRequest request,HttpServletResponse response){
        userAuthService.logout(request,response);
        return ResponseEntity.noContent().build();
    }

}
